package com.openthree.userport.service.daoservice;

import com.example.demo.util.JwtUtil;
import com.openthree.common.oss.CloudStorageService;
import com.openthree.common.oss.OSSFactory;
import com.openthree.userport.entity.PortImages;
import com.openthree.userport.entity.UserPort;
import com.openthree.userport.service.dao.PortImagesMapper;
import com.openthree.userport.service.dao.UserPortMapper;
import com.openthree.userport.vo.FormData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Service
public class PortService {

    @Autowired
    private UserPortMapper portMapper;
    @Autowired
    private PortImagesMapper portImagesMapper;

    /**
     * 保存作品和作品图片表
     * @param formData
     * @param
     * @return
     */
    @Transactional
    public String saveUserPort(FormData formData) throws Exception{
        UserPort userPort=new UserPort();
        userPort.setPortDesc(formData.getText());
        userPort.setUserId(JwtUtil.getLUserId());
        portMapper.insertSelective(userPort);
        return userPort.getId().toString();
    }
    /**
     *
     * 跟新作品图片表数据
     */
    public void savePortImages(FormData formData, MultipartFile file) throws Exception{
        CloudStorageService storageService= OSSFactory.build();
        String imageKey=JwtUtil.getLUserId()+"_"+formData.getPortId()+"_"+file.getOriginalFilename();
        String str=storageService.uploadObjectByInputStream(file.getInputStream(),"",imageKey );
        //保存图片信息
        PortImages portImages=new PortImages();
        portImages.setPortId(Long.parseLong(formData.getPortId()));
        portImages.setImageInfo(str);
        portImagesMapper.insertSelective(portImages);
    }
}




